local ActDefine = require("activity.Define")
local Util = require("common.Util")
local DailyActConditionLogic = require("activity.DailyActConditionLogic")
local ActivityExcel = require("excel.Activity")
local ActRuleConfig = ActivityExcel.dailyActRule
local WildBossConfig = ActivityExcel.wildboss

-----------------------------------
--开服活动的数据也保存再此
-----------------------------------

RunningActList = RunningActList or {}
CallBackList = CallBackList or {}

function onProcessActState(nData)
    if nData.actType == ActDefine.ACT_TYPE_WILDBOSS then
        if not RunningActList[nData.actType] then
            RunningActList[nData.actType] = {} 
        end
        RunningActList[nData.actType][nData.actArgs._configID] = nData 
        onCallBack(nData.actType, nData.status, nData.actArgs)
    else
        RunningActList[nData.actType] = nData 
        onCallBack(nData.actType, nData.status, nData.actArgs)
    end
end

function onCallBack(nActType, status, actArgs)
    local nCallBackData = CallBackList[nActType]
    if nCallBackData and nCallBackData[status] then
        nCallBackData[status](actArgs, nCallBackData.args)
    end
end

function onCheckActStatus(actType, actNum)
    if actType == ActDefine.ACT_TYPE_WILDBOSS then
        return (RunningActList[actType] and RunningActList[actType][actNum]) and RunningActList[actType][actNum].status or ActDefine.ACT_STATE_READY 
    else
        return RunningActList[actType] and RunningActList[actType].status or ActDefine.ACT_STATE_READY 
    end
end

function onCheckActCanJoin(human, actType, actNum)
    if onCheckActStatus(actType, actNum) ~= ActDefine.ACT_STATE_RUNNING then
        return false
    end

    if actType ~= ActDefine.ACT_TYPE_WILDBOSS then
        local nActRule = ActRuleConfig[RunningActList[actType].actArgs.ruleid]
        if not nActRule then
            return false
        end

        local nConditionResult = DailyActConditionLogic.onCheckCondition(nActRule.condition, human)
        if nConditionResult == false then
            return false
        end

        return true
    else
        local nWildBossConfig = WildBossConfig[RunningActList[actType][actNum].actArgs._configID]
        if not nWildBossConfig then
            return false
        end

        local nConditionResult = DailyActConditionLogic.onCheckCondition(nWildBossConfig.condition, human)
        if nConditionResult == false then
            return false
        end

        return true

    end
    return false
end

function onCheckActIsRunning(actType, actNum)
    if onCheckActStatus(actType, actNum) ~= ActDefine.ACT_STATE_RUNNING then
        return false
    end
    return true
end

function onCheckWhichBossIsRunning()
    if onCheckActIsRunning(ActDefine.ACT_TYPE_WORLDBOSS, nil) then
        local nActData = onGetActData(ActDefine.ACT_TYPE_WORLDBOSS, nil)
        if nActData then
            return nActData.actArgs._configID
        end
    end
    return ""
end

function onGetActData(actType, actNum)
    if not RunningActList[actType] then
        return nil
    end

    if actType == ActDefine.ACT_TYPE_WILDBOSS then
        if RunningActList[actType][actNum] then
            return RunningActList[actType][actNum]
        end
    else
        return RunningActList[actType]
    end
    return nil
end

function onRegisterCallBack(nActType, onBeginBefore, onRunning, onEnd, actArgs)
    CallBackList[nActType] = {[ActDefine.ACT_STATE_BEFORESTART] = onBeginBefore, [ActDefine.ACT_STATE_RUNNING] = onRunning, [ActDefine.ACT_STATE_ENDED] = onEnd, args = actArgs}
end

--function onProcessActState(nData)
--    RunningActList[nData.actType] = nData 
--end

--function onGetActStatus(actType)
--    return RunningActList[actType] and RunningActList[actType].status or ActDefine.ACT_STATE_READY 
--end
--
--function onCheckActCanJoin(human, actType)
--    if not onCheckActIsRunning(actType) then
--        return false
--    end
--
--    local nActRule = ActRuleConfig[RunningActList[actType].actArgs.ruleid]
--    if not nActRule then
--        return false
--    end
--
--    local nConditionResult = DailyActConditionLogic.onCheckCondition(nActRule.condition, human)
--    if nConditionResult == false then
--        return false
--    end
--
--    return true
--end
--
--function onGetActData(actType)
--    if RunningActList[actType] then
--        return RunningActList[actType]
--    end
--    return nil
--end
